package com.mobinters.cafeman;

import java.util.ArrayList;
import java.util.List;

import com.mobinters.cafeman.adapter.AreaAdapter;
import com.mobinters.cafeman.object.AreaItem;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;

public class LoginActivity extends Activity implements View.OnClickListener{
	
	EditText txtUserName,txtPassword;
	public static TextView txtArea;
	
	String[] areaList = new String[]{"Khu vuc 1",  "Khu vuc 2", "Khu vuc 3", "Khu vuc 4","Khu vuc 5" , "Khu vuc 6" ,"Khu vuc 7",
			"Khu vuc 8","Khu vuc 9"
	};
	List<AreaItem> mAreaList;
	SharedPreferences mPref;
	Editor mEditor;
	//List<String> mChosenArea;
	
	String checkedArea;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		setContentView(R.layout.login);
		
		mPref = getSharedPreferences(CMUtils.PREF_NAME , 0);
		mEditor = mPref.edit();
		
		
		txtUserName = (EditText) findViewById(R.id.txt_user_name);
		txtUserName.setText(mPref.getString(CMUtils.PREF_KEY_USERNAME, "waiter"));
		txtPassword = (EditText) findViewById(R.id.txt_password);
		txtPassword.setText(mPref.getString(CMUtils.PREF_KEY_PASSWORD, "123456"));
		txtArea = (TextView) findViewById(R.id.txt_chosen_area);
		txtArea.setOnClickListener(this);
		findViewById(R.id.btn_login).setOnClickListener(this);
		createAreaList();
		
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		switch(id){
		case R.id.btn_login:
			if(checkedArea == null) Toast.makeText(getBaseContext(), "Please choose area before login!", 
					Toast.LENGTH_SHORT).show();
			else{
					mEditor.putString(CMUtils.PREF_KEY_AREA, checkedArea);
					String userName = txtUserName.getText().toString();
					String passWord = txtPassword.getText().toString();
					mEditor.putString(CMUtils.PREF_KEY_USERNAME, userName);
					mEditor.putString(CMUtils.PREF_KEY_PASSWORD, passWord);
					mEditor.commit();
					CMActivity.startActivity(this); //after request user+ pwd
			}
			
		break;
		case R.id.txt_chosen_area:
			//show dialog for user to choose Area
			showAreaDialog(LoginActivity.this );//mAreaList
		break;
		
		}
		
	}
	
	private void createAreaList(){
		mAreaList = new ArrayList<AreaItem>();
		AreaItem temp;
		for(int i = 0; i<areaList.length; i++)
		{
			temp = new AreaItem();
			temp.Name = areaList[i];
			temp.Checked = false;
			
			mAreaList.add(temp);
		}
	}
	
	
	private void showAreaDialog(Context context){ //,final List<AreaItem> data
		final Dialog dialog = new Dialog(context);
		
		dialog.setTitle("Choose area");
		dialog.setContentView(R.layout.dlg_area);
		
		ListView lvArea = (ListView) dialog.findViewById(R.id.lv_area);				
		if(mAreaList!=null){
			AreaAdapter adapter = new AreaAdapter(context, mAreaList);
			lvArea.setAdapter(adapter);
		}
		
		dialog.findViewById(R.id.btn_area_cancel).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.dismiss();
				//resetData(mAreaList);
			}
		});
		
		dialog.findViewById(R.id.btn_area_ok).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.dismiss();
				checkedArea = getData(mAreaList);
				if(checkedArea != null ) LoginActivity.txtArea.setText(checkedArea);
			}
		});
		
		dialog.show();
		
	}
	
	
	private String getData(List<AreaItem> data){
		if(data == null) return null;
		StringBuilder sb = new StringBuilder();
		//mChosenArea = new ArrayList<String>();
		String name;
		for(int i = 0; i < data.size() ; i++){
			if(data.get(i).Checked) {	
					name = data.get(i).Name;
					sb.append( name + ",");
					//mChosenArea.add(name);
			}
		}
			
		
		return sb.toString();
	}

}
